package com.estate.struts.actions;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.*;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.jmesa.facade.TableFacade;
import org.jmesa.facade.TableFacadeImpl;
import org.jmesa.limit.ExportType;
import org.jmesa.limit.Limit;
import org.jmesa.view.component.Column;
import org.jmesa.view.component.Row;
import org.jmesa.view.component.Table;
import org.jmesa.view.editor.BasicCellEditor;
import org.jmesa.view.editor.CellEditor;
import org.jmesa.view.html.HtmlBuilder;
import org.jmesa.view.html.component.HtmlColumn;
import org.jmesa.view.html.component.HtmlRow;
import org.jmesa.view.html.component.HtmlTable;

import com.estate.business.TestService;
import com.estate.struts.actionForms.TestActionForm;
import com.estate.vo.Vo;
import com.estate.vo.impl.TestVo;
import com.opensymphony.xwork2.ActionSupport;

public class TestAction extends ActionSupport implements ServletRequestAware,ServletResponseAware {
	
	private TestService testService;
	private String test;
	private String method;
	
	private HttpServletRequest request;
    private HttpServletResponse response;
	
	@Override
	public String execute() throws Exception {
		System.out.println("action success");
		String page = "";
		System.out.println(method);
		if("list".equals(method)){
			List<Vo> voList = testService.getAllTest();
			ServletActionContext.getRequest().setAttribute("testList", voList);
			page = "list";
		}else if("export".equals(method)){
			System.out.println("export success");
			List<Vo> voList = testService.getAllTest();    
		        //<table id=”tableId”  border=”0″  cellpadding=”0″  cellspacing=”0″  class=”table”  width=”600px” >    
		        TableFacade tableFacade = new TableFacadeImpl("user_table", this.request);    
		        //设定页面分页数据    
		        tableFacade.setItems(voList);    
		        //设定支持的查询结果导出格式为csv,excel,pdf格式    
		        tableFacade.setExportTypes(this.response, ExportType.CSV, ExportType.JEXCEL, ExportType.PDF);    
		        tableFacade.setStateAttr("restore");    
		        Limit limit = tableFacade.getLimit();    
		        if (limit.isExported()) {    
		            export(tableFacade);  
		            System.out.println("export");
		            return null;    
		        } else {    
		            String html = html(tableFacade);    
		            this.request.setAttribute("user_table", html);    
		        }   
//			String id="user_table";
//	        TableFacade tableFacade = new TableFacadeImpl("user_table", this.request,this.response);
//	        System.out.println(tableFacade.getLimit());
			page = this.SUCCESS;
		}else if("ajax".equals(method)){
			
			
			
//			List<TestActionForm> list = new ArrayList<TestActionForm>();  
//            list.add(taf);   
//            list.add(taf2);   
			JSONObject obj = new JSONObject();
			HashMap<Integer,String> map=new HashMap<Integer,String>();
			map.put(1, "1");

			obj.putAll(map);
			System.out.println(obj.size());
			System.out.println("obj ========== " + obj.toString());
			obj.put("user", "2");
			 
            JSONArray root=new JSONArray();
//            JSONObject obj=new JSONObject();
            root.add(obj);
            
            String content = "(" + root.toString() + ")";
//			String content = "sdfdsf";
            System.out.println("content ========== " + content);
            PrintWriter out = this.response.getWriter();
            out.print(root);
            out.flush();
            out.close();
            page = null;
		}else{
			TestVo tv = new TestVo();
			tv.setTest(this.getTest());
			tv.setTestDate(null);
			testService.addTest(tv);
			
			page = this.SUCCESS;
		}
		return page;
	}
	
	 private String html(TableFacade tableFacade) {    
	        // 设定表格属性,注意此处的url用于诸如增加、删除、修改、查询操作，并不是实际的数据库表属性，    
	        //但表单需要有对应的po对新，因此需要在Person中增加此属性    
	        tableFacade.setColumnProperties("id", "firstName", "lastName", "url");    
	   
	        HtmlTable table = (HtmlTable) tableFacade.getTable();    
	        table.setCaption("测试用户信息列表");    
	        table.getTableRenderer().setWidth("600px");    
	   
	        HtmlRow row = table.getRow();    
	   
	        HtmlColumn id = row.getColumn("id");    
	        id.setTitle("id");    
	   
	        HtmlColumn firstName = row.getColumn("firstName");    
	        firstName.setTitle("属性1");    
	   
	        HtmlColumn lastName = row.getColumn("lastName");    
	        lastName.setTitle("属性2");    
	   
	        HtmlColumn deleteAction = row.getColumn("url");    
	        deleteAction.setTitle("操作");    
	   
	        // Using an anonymous class to implement a custom editor.    
	        // 用于演示在表格中增加超链接    
	        firstName.getCellRenderer().setCellEditor(new CellEditor() {    
	            public Object getValue(Object item, String property, int rowcount) {    
	                Object value = new BasicCellEditor().getValue(item, property,    
	                        rowcount);    
	                HtmlBuilder html = new HtmlBuilder();    
	                html.a().href().quote().append("http://www.mobile-soft.cn")    
	                        .quote().close();    
	                html.append(value);    
	                html.aEnd();    
	                return html.toString();    
	            }    
	        });    
	   
	   
	        return tableFacade.render(); // Return the Html.    
	    }    
	   
	    private void export(TableFacade tableFacade) {    
	        tableFacade.setColumnProperties("id", "test", "testDate");    
	   
	        Table table = tableFacade.getTable();    
	        table.setCaption("Persons Test");    
	   
	        Row row = table.getRow();    
	   
	        Column id = row.getColumn("id");    
	        id.setTitle("id");    
	   
	        Column firstName = row.getColumn("test");    
	        firstName.setTitle("First Name");    
	   
	        Column lastName = row.getColumn("testDate");    
	        lastName.setTitle("Last Name");    
	   
	        tableFacade.render();    
	    }    

	public String getTest() {
		return test;
	}

	public void setTest(String test) {
		this.test = test;
	}
	public TestService getTestService() {
		return testService;
	}

	public void setTestService(TestService testService) {
		this.testService = testService;
	}

	public String getMethod() {
		return method;
	}

	public void setMethod(String method) {
		this.method = method;
	}

	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
		
	}

	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
		
	}
}
